import {
	create,couponList
  } from '../../api/coupon'
  const util = require('../../utils/util.js')
  const app = getApp();

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		coupons:[],
		statusBarHeight:app.globalData.statusBarHeight,
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		this.getcouponList()
	},

	//查询平台优惠券列表
	getcouponList(){
		let _this = this
		let params = {
			mchId:app.globalData.mchId,
			isZero:1,
			sortField:'amount'
		}
		couponList(params).then(
			(res)=>{
				console.info("优惠券信息------")
				console.info(res)
				if (res.type == 'success') {
					let coupons = []
					if(res.data.data.length > 0){
						this.initCoupons(res.data.data)
					}
				}
			}
		).catch( )
	},

	initCoupons(data){
		let _this = this
		let coupons = []
		data.map(item => {
			// if(item.stock>0){
			// 	if(item.stock - item.canStock < item.maxgiven){
			// 		item.canReceive = true
			// 	}else{
			// 		item.canReceive = false
			// 	}	
			// }
			item.endDate = util.datetimeFormat(item.endDate)
		})
		_this.setData({
			coupons :data 
		})
		console.info("this.data.coupons***********************")
		console.info(this.data.coupons)
	},

	//领取优惠券
	maskClick(e) {
		let _this = this
		if (app.globalData.token == null) {
		  wx.navigateTo({
			url: '/pages/login/index',
		  })
		} else {
		  create({
			couponId: e.currentTarget.dataset.id,
			mchId: app.globalData.mchId
		  }).then(res => {
			wx.showToast({
			  title: '领取成功',
			  icon: 'success',
			  duration: 2000
			})
			_this.getcouponList()
		  })
		}
	  },
})